<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        static::customerSeeder();
        static::categorySeeder(null, config('init.category'));
        static::attributeSeeder(config('init.attribute'));
        static::tagSeeder(config('init.tag'));
        $this->call(ChinaAreaTableSeeder::class);

        $this->laravelAdminSeeder();

        \App\User::query()->create([
            'name' => 'administrator',
            'email' => 'nfangxu@gmail.com',
            'password' => bcrypt('123456'),
        ]);
        $this->call(ExpCompaniesTableSeeder::class);
    }

    private function seedProduct()
    {

        factory(\App\Models\Coupon::class, 50)->create();

        factory(\App\Models\Carousel::class, 10)->create()
            ->each(function (\App\Models\Carousel $carousel) {
                factory(\App\Models\Image::class)->create([
                    'type_id' => $carousel->id,
                    'type' => \App\Models\Image::CAROUSEL,
                ]);
            });
        factory(\App\Models\Notice::class, 20)->create()
            ->each(function (\App\Models\Notice $notice) {
                factory(\App\Models\Image::class)->create([
                    'type_id' => $notice->id,
                    'type' => \App\Models\Image::NOTICE,
                ]);
            });
        \App\Models\Customer::all()->each(
            function (\App\Models\Customer $customer) {
                factory(\App\Models\UserAddress::class, random_int(1, 3))
                    ->create(['customer_id' => $customer->id]);
            }
        );

        $this->call(ProductSeeder::class);
    }

    private function laravelAdminSeeder()
    {
        $this->call(AdminMenuTableSeeder::class);
        $this->call(AdminPermissionsTableSeeder::class);
        $this->call(AdminRoleMenuTableSeeder::class);
        $this->call(AdminRolePermissionsTableSeeder::class);
        $this->call(AdminRolesTableSeeder::class);
        $this->call(AdminRoleUsersTableSeeder::class);
        $this->call(AdminUserPermissionsTableSeeder::class);
        $this->call(AdminUsersTableSeeder::class);
    }

    private static function categorySeeder($parent, $children)
    {
        foreach ($children as $name => $child) {
            $category = \App\Models\Category::query()->create([
                'name' => $name,
                'summary' => $name,
                'parent_id' => $parent ? $parent->id : 0,
            ]);
            factory(\App\Models\Image::class)->create([
                'type_id' => $category->id,
                'type' => \App\Models\Image::CATEGORY_COVER,
            ]);

            if ($child) {
                self::categorySeeder($category, $child);
            }
        }
    }

    private static function attributeSeeder($data)
    {
        foreach ($data as $datum) {
            \App\Models\Attribute::query()->create([
                'name' => $datum,
            ]);
        }
    }

    private static function tagSeeder($data)
    {
        foreach ($data as $datum) {
            \App\Models\Tag::query()->create([
                'name' => $datum,
            ]);
        }
    }

    private static function customerSeeder()
    {
        $customer = new \App\Models\Customer();

        $customer->username = "customer";
        $customer->mobile = "13212341234";
        $customer->password = bcrypt('123456');
        $customer->verify_at = now();
        $customer->save();
    }
}
